/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rppowell.pixie.swing.sprite;

import com.rppowell.pixie.models.Sprite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author robert.powell
 */
public class SpriteCanvasPanel extends JPanel  {
    private int scaleMultiplier;
    private Sprite sprite;
    private Color background;
    private Rectangle canvasRectangle;
    private Rectangle spriteRectangle;
    
    public SpriteCanvasPanel() {
        //setBorder(BorderFactory.createLoweredSoftBevelBorder());
        spriteRectangle = new Rectangle();
        canvasRectangle = new Rectangle();
        scaleMultiplier = 10;
        setBorder(BorderFactory.createLoweredBevelBorder());        
    }

    private void updateRectangles() {
        if (canvasRectangle == null) {
            canvasRectangle = new Rectangle();
        }
        if (spriteRectangle == null) {
            spriteRectangle = new Rectangle();
        }
        if (sprite == null) {
            canvasRectangle.width = 0;
            canvasRectangle.height = 0;
        } else {
            Dimension x = getSize();
            Insets i = getInsets();
            int w = x.width - i.left - i.right;
            int h = x.height - i.top - i.bottom;
            
            spriteRectangle.width = sprite.getWidth() * scaleMultiplier;
            spriteRectangle.height = sprite.getHeight() * scaleMultiplier;
            spriteRectangle.x = Math.max(i.left, (w - spriteRectangle.width)/2);
            spriteRectangle.y = Math.max(i.top , (h - spriteRectangle.height)/2);
            
            canvasRectangle.width = (int)(spriteRectangle.width * 1.5);
            canvasRectangle.height = (int)(spriteRectangle.height * 1.5);
            canvasRectangle.x = Math.max(i.left, (w - canvasRectangle.width)/2);
            canvasRectangle.y = Math.max(i.top, (h - canvasRectangle.height)/2);
        }
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g); //To change body of generated methods, choose Tools | Templates.
        updateRectangles();
        Insets i = getInsets();
        Dimension d = getSize();
        Color background = getBackground();
        Color c;
        if (sprite != null) {
            c = Color.BLACK;
            g.setColor(c);
            g.fillRect(i.left, i.top, d.width - i.left - i.right, d.height - i.top - i.bottom);
            
            c = Color.BLUE;
            g.setColor(c);
            g.fillRect(canvasRectangle.x, canvasRectangle.y, canvasRectangle.width, canvasRectangle.height);
            
            g.drawImage(sprite,
                    spriteRectangle.x, spriteRectangle.y, spriteRectangle.x+spriteRectangle.width, spriteRectangle.y+spriteRectangle.height,
                    0, 0, sprite.getWidth(), sprite.getHeight(),
                    this);
        } else {
            
            c = Color.BLACK;
            g.setColor(c);
            g.fillRect(i.left, i.top, d.width - i.left - i.right, d.height - i.top - i.bottom);
            
        }
    }
    
    public void setSelectedSprite(Sprite sprite) {
        this.sprite = sprite;
        System.out.println("Sprite Selected");
        updateRectangles();
        this.setPreferredSize(new Dimension(canvasRectangle.width, canvasRectangle.height));
        this.invalidate();
        this.repaint();
    }
}
